package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;
    
    /**
     * Contructor for objecs of this class.
     */
    public AddressBookUI()
    {
       addrCtr = new AddressCtr();
    }
    
    /**
     * This method starts  the showing of this menu.
     */
    public void start()
    {
        clscr();
        addressMenu();
    }
    
    /**
     * This method writes the address book menu, and handles the choise based on it.
     */
    private void addressMenu()
    {
       boolean exit = false;
       while(!exit)
       {
          int choise = writeAddressBookMenu(); 
          // replaced the previous solution with a switch, it is more managable.
          switch(choise)
          {
              case 1:
                getPerson();
                break;
              case 2:
                createPerson();
                break;
              case 3:
                deletePerson();
                break;
              case 4:
                updatePerson();
                break;
              case 5:
                listAllPersons();
                break;
              case 6:
                exit = true;
                break;
                
          }
       }
    }
    
    /**
     * Thiis method writes the address book menu and handles the keyboard input for the choise.
     */
    private int writeAddressBookMenu()
    {
            System.out.println("");
            System.out.println("");
            System.out.println(" *** AddressBook Menu *** ");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            
            int choise = 0;
            while(choise == 0)
            {
                try
                {
                    Scanner keyboard = new Scanner(System.in);
                    System.out.println("\n Make your choise: ");
                    choise = keyboard.nextInt();
                }
                catch(Exception e)
                {
                    System.out.println("Invalid input, try again.");
                }
            }
            return choise;
    }
    
    /**
     * This method prints a string to the terminal an then returns a long from the keyboard.
     */
    private long inputLong(String str)
    {
        long input = 0;
        while(input == 0)
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                System.out.println(str);
                input = keyboard.nextLong();
            }
            catch(Exception e)
            {
                System.out.println("Invalid input, try again.");
            }
        }
        return input;
    }
    
    /**
     * This method prints a string to the terminal an then returns a String form the keyboard
     */
    private String inputLine(String str)
    {
        String input = "";
        while(input.equals(""))
        {
            try
            {
                Scanner keyboard = new Scanner(System.in);
                System.out.println(str);
                input = keyboard.nextLine();
            }
            catch(Exception e)
            {
                System.out.println("Invalid input, try again.");
            }
        }
        return input;
    }
    
    /**
     * This method show all information about a person
     */
    private void showPersonDetails(Person p)
    {
        System.out.println("Id:             " + p.getId());
        System.out.println("Name:           " + p.getFirstName() + " " + p.getSurName());
        System.out.println("Address:        " + p.getAddress());
        System.out.println("Postal code:    " + p.getZip());
        System.out.println("City:           " + p.getTown());
        System.out.println("Phone#:         " + p.getPhone());
        
    }

    /**
     * This method method request the id of a person from the user, and the displays the available details a said person.
     */
    private void getPerson()
    {
        long id = inputLong("Write Id:  ");
        clscr();
        try
        {
            Person p = addrCtr.getPerson(id);
            showPersonDetails(p);
        }
        catch(Exception e)
        {
            System.out.println("Person does not exist.");
        }
    }   
    
    /**
     * This method creates a new person based on that persons name, address and phone number. These data is requested of the user.
     */
    private void createPerson()
    {
        String firstName = inputLine("Write first name:  ");
        String lastName = inputLine("Write last name:  ");
        String address = inputLine("Write Address: ");
        String zip = inputLine("Write Postal Code:  ");
        String town = inputLine("Write City:  ");
        String phone = inputLine("Write Phone Number:  ");
        
        addrCtr.createPerson(firstName,lastName,address,zip,town,phone);
        clscr();
        System.out.println("A new person whas created.");
    }
    
    /**
     * This method request the id of a person to be deleted of the user. it then deletes said person.
     */
    private void deletePerson()
    {
        long id = inputLong("Write Id:  ");
        boolean succes = addrCtr.deletePerson(id);
        clscr();
        if(succes)
        {
            System.out.println("Person with id " + id + " was deleted.");
        }
        else
        {
            System.out.println("Could not find a person with id: " + id);
        }
    }
    
    /**
     * This method request details about a person, and then updates them.
     */
    private void updatePerson()
    {
        long id = inputLong("Write Id:  ");
        String firstName = inputLine("Write first name:  ");
        String lastName = inputLine("Write last name:  ");
        String address = inputLine("Write Address: ");
        String zip = inputLine("Write Postal Code:  ");
        String town = inputLine("Write City:  ");
        String phone = inputLine("Write Phone Number:  ");
        
        boolean succes = addrCtr.updatePerson(id,firstName,lastName,address,zip,town,phone);
        clscr();
        if(succes)
        {
            System.out.println("Person was updated.");
            try
            {
                Person p = addrCtr.getPerson(id);
                showPersonDetails(p);
            }
            catch(Exception e)
            {
            }
        }
        else
        {
            System.out.println("Could not find a person with id: " + id);
        }
    }
    
    /**
     * This method writes details about all the person in the address book.
     */
    private void listAllPersons()
    {
        ArrayList<Person> list = addrCtr.listAllPersons();
        clscr();
        for(Person p : list)
        {
            showPersonDetails(p);
            System.out.println("");
        }
    }
    /**
     * Practical little support method that clears the terminal of text.
     */
    private void clscr()
    {
        System.out.println("\f");
    }
}